package se.webbzon.boltzmann.minigame.sandbox;

import java.awt.Color;
import java.awt.Font;

import se.webbzon.boltzmann.resource.Resources;
import se.webbzon.oschi01.font.HorizontalAlignment;
import se.webbzon.oschi01.font.VerticalAlignment;
import se.webbzon.oschi01.worldarea.WorldArea;
import se.webbzon.oschi01.worldcamera.WorldCameraPane;
import se.webbzon.oschi01.worldobject.WorldObject;

public class SandBoxText extends DefaultSandBoxItem implements SandBoxAnimatable {
	
	// The 2-dimensional depth of this sand box text
	private static final int depth = 2;
	
	// The default font of this sand box text
	private static final Font defaultFont = Resources.getFont("larabie_plain");
	
	// The current animation of this sand box text
	private SandBoxAnimation animation;
	
	// The font of the text
	private Font font;
	
	// The color of the text
	private Color color;
	
	// The transparency of the text
	private float transparency;
	
	// The alignment of the text
	private HorizontalAlignment hAlign;
	private VerticalAlignment vAlign;
	
	// The text of this sand box text
	private String text;
	
	/*============================================================
	Constructors
	============================================================*/
	
	public SandBoxText(String text) {
		this.text = text;
		hAlign = HorizontalAlignment.LEFT;
		vAlign = VerticalAlignment.TOP;
		color = Color.BLACK;
		transparency = 1;
		font = defaultFont;
		setInstance(new Instance());
	}
	
	/*============================================================
	Public Methods
	============================================================*/
	
	/** Returns the font of this sand box text. **/
	public Font getFont() {
		return font;
	}
	
	/** Sets the font of this sand box text. **/
	public void setFont(Font font) {
		this.font = font;
	}
	
	/** Returns the color of the text. **/
	public Color getColor() {
		return color;
	}
	
	/** Sets the color of the text. **/
	public void setColor(Color color) {
		this.color = color;
	}
	
	/** Returns the horizontal alignment of this sand box text. **/
	public HorizontalAlignment getHorizontalAlignment() {
		return hAlign;
	}

	/** Sets the horizontal alignment of this sand box text. **/
	public void setHorizontalAlignment(HorizontalAlignment hAlign) {
		this.hAlign = hAlign;
	}

	/** Returns the vertical alignment of this sand box text. **/
	public VerticalAlignment getVerticalAlignment() {
		return vAlign;
	}

	/** Sets the vertical alignment of this sand box text. **/
	public void setVerticalAlignment(VerticalAlignment vAlign) {
		this.vAlign = vAlign;
	}
	
	@Override public void setSize(double width, double height) {
		super.setSize(width, height);
	}
	
	@Override public SandBoxAnimation getAnimation() {
		return animation;
	}

	@Override public void setAnimation(SandBoxAnimation animation) {
		this.animation = animation;
	}

	@Override public void setTransparency(float transparency) {
		this.transparency = transparency;
	}

	@Override public float getTransparency() {
		return transparency;
	}

	@Override public void setRotation(double rotation) {};

	@Override public double getRotation() {
		return 0;
	}
	
	/*============================================================
	Private Classes
	============================================================*/
	
	private final class Instance extends SandBoxObject {

		public Instance() {
			super(WorldObject.onPostDraw + WorldObject.onStepEnd, depth);
		}

		@Override public void onAttach(SandBox box) {};

		@Override public void onDetach(SandBox box) {};

		@Override public SandBoxIcon createIcon() {
			return null;
		}

		@Override public void onStepEnd(WorldArea world) {
			if (animation != null)
				animation.onStepEnd(SandBoxText.this);
		}

		@Override public void onPostDraw(WorldArea world, WorldCameraPane hud) {
			hud.setHAlignment(hAlign);
			hud.setVAlignment(vAlign);
			hud.setColor(color);
			if (font != null) {
				Font f = font.deriveFont((float) getHeight());
				hud.setFont(f);
			}
			if (transparency != 1)
				hud.setTransparency(transparency);
			hud.drawString(text, (int) getX(), (int) getY());
			if (transparency != 1)
				hud.setTransparency(1);
		}
		
	}

}
